package com.yztl.controller;


import com.alibaba.fastjson.JSON;
import com.yztl.pojo.Admin;
import com.yztl.pojo.Client;
import com.yztl.pojo.Order;
import com.yztl.service.admin.AdminService;
import com.yztl.util.PageUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("/login.html")
    public String login(){
        return "AdminLogin";
    }

    @PostMapping("/doLogin.html")
    public String doLogin(@RequestParam String username,
                          @RequestParam String password,
                          HttpSession session){
        Admin admin = adminService.AdminLogin(username, password);
        if(admin!=null){
            session.setAttribute("Admin",admin);
            session.removeAttribute("error");
            return "redirect:/admin/main.html";
        }else{
            session.setAttribute("error","账号或密码错误");
            return "AdminLogin";
        }

    }
    @RequestMapping("/main.html")
    public String main(HttpSession session){
        if(session.getAttribute("Admin")==null){
            return "AdminLogin";
        }
        return "ShowClient";
    }

    @RequestMapping("/over.html")
    public String over(HttpSession session){
            session.removeAttribute("Admin");
            session.removeAttribute("error");
            return "index";
    }

    @RequestMapping("/getPageClient.html")
    @ResponseBody
    public String getPageClient(@Param("pageIndex") String pageIndex,HttpSession session){
        if(pageIndex==null ){
            pageIndex="1";
        }
        int index=Integer.parseInt(pageIndex);
        if(index<=1){
            index=1;
        }
        Admin admin = (Admin) session.getAttribute("Admin");
        int id=admin.getAdmin_id();
        PageUtil<Client> pageUtil = new PageUtil<>();
        pageUtil.setPageSize(3);
        pageUtil.setPageIndex(index);
        adminService.getPageClient(pageUtil,id);
        List<Client> lists = pageUtil.getLists();
        session.setAttribute("lists",lists);
        return JSON.toJSONString(pageUtil);
    }

    @RequestMapping("/delOrder.html")
    @ResponseBody
        public String delOrder(int id,int state){
        return JSON.toJSONString(adminService.deleteOrder(id,state));
        }



    @RequestMapping("/getProduct.html")
    @ResponseBody
    public String getProduct(){
        return JSON.toJSONString(adminService.getProduct());
    }

    @RequestMapping("/getStatus.html")
    @ResponseBody
    public String getStatus(){
        return JSON.toJSONString(adminService.getStatus());
    }

    @RequestMapping("/goSelOne.html")
    public String goSelOne(int id, HttpSession session){
        session.setAttribute("id",id);
        return "updClient";
    }
    @RequestMapping("/selOne.html")
    @ResponseBody
    public  String selOne(int id,HttpSession session){
        Client client = adminService.selOne(id);
        session.setAttribute("client",client);
        return JSON.toJSONString(client);
    }

    @RequestMapping("/upd.html")
    public String upd(
                    HttpSession session, String customer_name, String customer_phone, int product_name, int product_num, int order_state
                              ){
        Client client = (Client) session.getAttribute("client");
        int clientId = 0;
        for (Order order : client.getOrder()) {
          clientId = order.getOrder_userid();
        }
        int updClient= adminService.updClient(new Client(clientId, customer_name, customer_phone));
        int updOrder = adminService.updOrder(new Order(client.getId(),product_name,product_num,new Date(),order_state));
        if(updClient !=0 && updOrder!=0){
            return "ShowClient";
        }else{
            return "updClient";
        }
    }

    @RequestMapping("/goAdd.html")
    public String goAdd(){
        return "AddClient";
    }

    @RequestMapping("/add.html")
    public String add(HttpSession session, String customer_name, String customer_phone, String customer_address
            ){
        Admin admin = (Admin) session.getAttribute("Admin");
        int customer_adminId = admin.getAdmin_id();
        if(adminService.addClient(new Client(customer_name,customer_phone,"666",customer_address,customer_adminId))!=0){
            return "ShowClient";
        }else{
            return "AddClient";

        }

    }


    }






